package com.tj.jinzao.activity;


import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseActivity;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.widgets.ChooseConfigDialog;

import butterknife.BindView;


/**
 * 人脸识别样式activity
 * Created by wjy on 2019/1/5.
 */
public class FaceDetectDemoActivity extends BaseActivity {

    @BindView(R.id.iv_head)
    ImageView mHeadView;
    @BindView(R.id.iv_scan)
    ImageView mScanView;
    @BindView(R.id.title)
    TextView title;

    private int mType;
    private ChooseConfigDialog mDialog;


    private void dialog(String content){
        mDialog = new ChooseConfigDialog(FaceDetectDemoActivity.this, content);
        mDialog.show();
        mDialog.setOnClickLinstener(new ChooseConfigDialog.OnClickLinstener() {
            @Override
            public void onConfig(String mContent) {
                mDialog.dismiss();
                if(mContent.equals("1")){
                    finish();
                }

            }
        });
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if(mDialog!=null&&mDialog.isShowing()){
                return true;
            }
            dialog("退出，检测流程全部终止");
        }
        return true;

    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_face_detect_demo;
    }

    @Override
    protected void setupView() {
        super.setupView();
        setStausBar(getResources().getColor(R.color.white));
        mType = getIntent().getIntExtra("step", 0);
        title.setText("面诊1/3");
        if (mType == 1) {
            title.setText("舌诊2/3");
            mHeadView.setImageResource(R.mipmap.face_tongue);
        }
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                if(!mOver){
                    startActivity(new Intent(FaceDetectDemoActivity.this, mType == 0 ? FaceDetectActivity.class : TongueDetectActivity.class));
                    finish();
                }
            }
        };
        findViewById(R.id.back_lay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        ObjectAnimator translationYAnim = ObjectAnimator.ofFloat(mScanView, "translationY", -Constant.SCREEN_WIDTH / 2, Constant.SCREEN_WIDTH / 2, -Constant.SCREEN_WIDTH / 2);
        translationYAnim.setDuration(5000);
        translationYAnim.setRepeatCount(ValueAnimator.INFINITE);
//        translationYAnim.setRepeatMode(ValueAnimator.REVERSE);
        translationYAnim.start();
        AnimatorSet set = new AnimatorSet();
        set.play(translationYAnim);
        set.start();
        mHeadView.postDelayed(runnable, 5000);
    }

    private boolean mOver = false;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mOver = true;

    }
}
